package com.wxlgroup.springboot.controller;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.wxlgroup.springboot.commons.Response;
import com.wxlgroup.springboot.dao.EquipmentDao;
import com.wxlgroup.springboot.entity.DElink;
import com.wxlgroup.springboot.service.EquipmentService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;;
import java.net.URLEncoder;
import java.util.List;

@RestController//测试git
@RequestMapping("/equipment")
public class EquipmentController {

    @Resource
    private EquipmentService equipmentService;

    @Resource
    private EquipmentDao equipmentDao;



//    @GetMapping("/page")
//    public Response queryAllEquipment(int pageNumber,int pageSize){
//        Object data = equipmentService.queryAllEquipments(pageNumber, pageSize);
//        if(data!=null){
//            return new Response("200",data,"查询成功");
//        }else {
//            return new Response("500",null,"查询失败,无设备");
//        }
//
//    }

    @GetMapping("/page")
    public Response queryAllEquipments(int pageNumber,int pageSize,String equipmentName,String departmentName){
        Object data = equipmentService.queryAllEquipments(pageNumber, pageSize,equipmentName,departmentName);
        if(data!=null){
            return new Response("200",data,"查询成功");
        }else {
            return new Response("500",null,"查询失败,无设备");
        }

    }

    @PostMapping("/update")     //修改
    public Response updateEquipment(@RequestBody DElink dElink){
        return equipmentService.updateEquipment(dElink);
    }


    @DeleteMapping("/delete/{id}")      //删除单个设备信息
    public Response deleteById(@PathVariable int id){
        return equipmentService.deleteEquipmentById(id);
    }


    @PostMapping("/deleteEquipments")       //删除多个设备信息
    public Response deleteEquipments(@RequestBody List<Integer> ids){
        return equipmentService.deleteEquipments(ids);
    }


    @PostMapping("/insert")     //新增
    public Response insertEquipment(@RequestBody DElink addEquipmentFrom){
        return equipmentService.updateEquipment(addEquipmentFrom);
    }


    @GetMapping("/export")
    public void exportEquipments(HttpServletResponse response) throws IOException {
        List<DElink> dElinks = equipmentDao.export();
        // 在内存操作，写出到浏览器
        ExcelWriter writer = ExcelUtil.getWriter(true);
        // 一次性写出list内的对象到excel，使用默认样式，强制输出标题
        writer.write(dElinks, true);
        // 设置浏览器响应的格式
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        String fileName = URLEncoder.encode("设备记录相关信息", "UTF-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx");
        ServletOutputStream out = response.getOutputStream();
        writer.flush(out, true);
        out.close();
        writer.close();
    }
}
